<?php
/**
 * Created by PhpStorm.
 * User: Talent Gao
 * Date: 14-8-19
 * Time: 下午1:43
 */


class IndexController extends Controller {


    public function init() {
        parent::init();
    }

    public function indexAction(){
        global $_PurchaseOrderCfg,$_BuyStateCfg,$_suppliyStateCfg;
        define('ITEM_PER_PAGE', 10);


        //自营现货
        $vRmb =array(
            'currency_code' => 'RMB',
            'region'  => '',
            'pageSize'    => ITEM_PER_PAGE,
        );
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";
        $pRmb = Tools::https_request($url,$vRmb);
        $lRmb = json_decode($pRmb,true);


        $vUsd =array(
            'currency_code' => 'USD',
            'region'  => '',
            'pageSize'    => ITEM_PER_PAGE,
        );
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";
        $pUsd = Tools::https_request($url,$vUsd);
        $lUsd = json_decode($pUsd,true);

        foreach($lRmb['data'] as $key=>$var){
            $lRmb['data'][$key]['create_time'] = Tools::time2Units(time() - strtotime($var['create_time'])) . "前";
        }
        foreach($lUsd['data'] as $key=>$var){
            $lUsd['data'][$key]['create_time'] = Tools::time2Units(time() - strtotime($var['create_time'])) . "前";
        }

        $price =  array(
            'RMB' => $lRmb['data'],
            'USD' => $lUsd['data'],
        );


        //求购
        $start=1;
        $where ="iq_inquiry.status in('{$_BuyStateCfg['audit']}',{$_BuyStateCfg['Negotiate']})";
        $order ="iq_inquiry.update_time DESC";
        $buy = new BuyModel();
        $buylist = $buy->getPageListByWhere($start,ITEM_PER_PAGE,$where,$order);
        foreach($buylist as $key=>$val){
            $buylist[$key]['update_time'] = Tools::time2Units(time () - strtotime ( $val['update_time'] )) . "前";
        }

        //供应

        $Supply = new SupplyModel();
        $page =1;
        $where ="iq_quotation.type='outer' and iq_quotation.status in('{$_suppliyStateCfg['up']}')";
        $orderby = "iq_quotation.update_time desc";

        $supplylist = $Supply->getPageListByWhere($page,ITEM_PER_PAGE, $where,$orderby);

        foreach($supplylist as $key=>$val){
            $supplylist[$key]['update_time'] = Tools::time2Units(time() - strtotime($val['update_time'])) . "前";
        }

        $this->_view->price = $price;
        $this->_view->buylist  = $buylist;
        $this->_view->supplylist =$supplylist;


    }
}